<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="商品" prop="name">
        <el-select
          v-model="formData.name"
          filterable
          placeholder="请选择商品"
        >
          <el-option
            v-for="item in commodityList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="数量" prop="count">
        <el-input v-model.number="formData.count" placeholder="请输入数量" />
      </el-form-item>
      <el-form-item label="备注" prop="description">
        <el-input v-model="formData.description" type="textarea" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { InventoryApi, InventoryVO } from '@/api/wms/inventory'
import { CommodityApi, CommodityVO } from "@/api/wms/commodity";

/** 库存 表单 */
defineOptions({ name: 'InventoryForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref() // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：in - 入库；out - 出库
const formData = ref({
  id: undefined,
  cid: undefined,
  count: undefined,
  location: undefined,
  name: undefined,
  wid: '',
  description: undefined,
})
const formRules = reactive({
  name: [
    { required: true, message: '商品不能为空', trigger: 'change' },
  ],
  count: [
    { required: true, message: '商品数量不能为空', trigger: 'blur' },
    {
      type: 'number',
      min: 1, // 这里设置最小值为 1
      message: '商品数量必须大于 0', // 统一提示信息
      trigger: 'blur'
    }
  ],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: string) => {
  dialogVisible.value = true
  // dialogTitle.value = t('action.' + type)
  if  (type === 'in') {
    dialogTitle.value = '入库'
  } else if (type === 'out') {
    dialogTitle.value = '出库'
  }
  formType.value = type
  resetForm()

  // 设置仓库id
  // console.log(id)
  formData.value.wid = id as string
  // console.log(formData.value.wid)
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    let params = ref({
      cid: formData.value.name,
      count: formData.value.count,
      description: formData.value.description,
      wid: formData.value.wid,
    })
    const data = params.value as unknown as InventoryVO
    if (formType.value === 'in') {
      await InventoryApi.inInventory(data)
      message.success('入库商品成功！')
    } else if (formType.value === 'out') {
      await InventoryApi.outInventory(data)
      message.success('出库商品成功！')
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    cid: undefined,
    count: undefined,
    location: undefined,
    name: undefined,
    wid: '',
    description: undefined,
  }
  formRef.value?.resetFields()
}

/** 获取“商品”选择器数据 */
const commodityList = ref<CommodityVO[]>([])
const getCommodityList = async () => {
  const data = await CommodityApi.getCommodityList()
  // console.log(data)
  commodityList.value = data
}

/** 钩子函数 */
onMounted(() => {
  getCommodityList()
})
</script>
